!function (e, $, mui) {
    "use strict";
	
	var config = {
	};
	// 定义页面
	var p = function(config) {
		var t = this;
		t.config = config;
		t.userInfo = null;
		t.element = {
			title: $('.post_title'),
			no: $('.post_no'),
			create_time: $('.post_create_time'),
			content: $('.post_content'),
			photos: $('.post_photos'),
			apply: $('.action .apply'),
			cancel: $('.action .cancel'),
			apply_time: $('.apply_time'),
			apply_status: $('.apply_status')
		};
		t.init();
		t.listen();
	};
	
	var pt = p.prototype;
	
	// 页面初始化
	pt.init = function () {
		var t = this;
		t.userInit();
		t.pageLoad();
	};
	
	// 用户信息初始化
	pt.userInit = function() {
		var t = this;
		t.userInfo = getLocalUserInfo();
	};
	
	// 页面加载
	pt.pageLoad = function () {
		var t = this;
		t.element.apply_time.parent().hide();
		t.element.apply_status.parent().hide();
		baseApi.get(
			'lost/info',
			{id: getQueryString('id') || 10},
			function (result) {
				t.info = result;
				t.element.title.html(result.post_title);
				t.element.create_time.html(result.create_time_format);
				t.element.no.html(result.post_no);
				t.element.content.html(result.post_content);
				
				if(result.more && result.more.photos) {
					result.more.photos.forEach(function (item) {
						t.element.photos.append('<img src="'+item.url+'" />');
					});
				}
				
				// 待申请
				if(result.post_status == 1) {
					t.element.apply.show();
				} else if(result.apply.user_id == t.userInfo.id) {
					t.element.apply_time.text(result.apply.create_time_format);
					t.element.apply_status.text(result.apply.status_format);
					t.element.apply_time.parent().show();
					t.element.apply_status.parent().show();
					
					if(result.apply.status == 1) {
						t.element.cancel.show();
					} else {
						
					}
				}
			}
		);
	}
	
	// 监听
	pt.listen = function () {
		var t = this;
		t.element.photos.on('tap', 'img', function () {
			var url = $(this).attr('src');
			openBigImage(url, true);
		});
		t.element.apply.on('tap', function () {
			if(!t.userInfo.user_realname || !t.userInfo.mobile) {
				return mui.alert('请完善个人信息后申请', '温馨提示', '去完善', function () {
					location.href = '../user/edit.html';
				});
			}
			baseApi.get(
				'lost/apply',
				{id: getQueryString('id')},
				function (result) {
					mui.toast(result);
					setTimeout(function () {
						location.reload();
					}, 500);
				}
			);
		});
		t.element.cancel.on('tap', function () {
			baseApi.get(
				'lost/cancel',
				{id: t.info.apply.id},
				function (result) {
					mui.toast(result);
					setTimeout(function () {
						location.reload();
					}, 500);
				}
			);
		});
	}
	
	// 页面初始化
	e.page = new p(config);

}(window, $, mui);